package com.joint.web.action.mp;

import com.fz.us.base.service.common.ResultService;
import com.joint.base.entity.Users;
import com.joint.base.service.AdminService;
import com.joint.base.service.UsersService;
import com.joint.core.service.AdminInfoService;
import com.joint.web.action.BaseController;
import fz.me.chanjar.weixin.common.api.WxConsts;
import fz.me.chanjar.weixin.common.exception.WxErrorException;
import fz.me.chanjar.weixin.mp.api.WxMpService;
import fz.me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 后台Action类 - 网站注册 - 登录注册
 * ============================================================================
 * 版权所有 2013 min_xu。
 * ----------------------------------------------------------------------------
 * 
 * @author min_xu
 * 
 * @version 0.1 2013-7-15
 *
 * 方法需要封装到adminService里面去。
 */

@Controller
@ParentPackage("mp")
public class AccountAction extends BaseController {
	private static final long serialVersionUID = -5383463207248344967L;


    @Resource
    private AdminService adminService;
    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private UsersService usersService;
    @Resource
    private ResultService resultService;
    @Resource
    private WxMpService wxMpService;

    private Users users;
    //参数
    private String username;
    private String password;
    private String passwordConfirm;
    private String authcode;
    private String terms;
    private String type;
    private String experienceAdminAccount;
    private int expireDay;

    private String openId;
    private String from;

    private int ifWxUsers;
    private String nickname;
    private String headimgurl;


    private String salt = "c74894d1197f0a6505f82a348996a0f4";

    @Action("/mp/account/connectMp")
    public Map<String,Object> connectMp(){
         return ajaxResult(resultService.success("绑定成功"));
    }
    /**
     * 如果没有权限则直接跳转到登录界面
     * */
    public String resetMp() throws Exception {
        users = usersService.getLoginInfo();
        openId = usersService.openId(users);
        expireDay = adminInfoService.getDaysByUsers(users);
        ifWxUsers = usersService.isWxUsers(openId)==true?1:0;

        if(StringUtils.isNotEmpty(openId)){
            try {
                WxMpUser mpUser = wxMpService.userInfo(openId, WxConsts.CN);
                nickname = mpUser.getNickname();
                headimgurl = mpUser.getHeadImgUrl();
            }catch (WxErrorException e){

            }finally {

            }
        }

        if(StringUtils.isEmpty(nickname)){
            //跳转到login重新绑定
            nickname = "账号绑定异常，如有问题请重置账号。";
            headimgurl = "../resource/com/img/avatars/male.png";
            return "mplogin";
        }
        return "reset";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public String getAuthcode() {
        return authcode;
    }

    public void setAuthcode(String authcode) {
        this.authcode = authcode;
    }

    public String getTerms() {
        return terms;
    }

    public void setTerms(String terms) {
        this.terms = terms;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getExperienceAdminAccount() {
        return experienceAdminAccount;
    }

    public void setExperienceAdminAccount(String experienceAdminAccount) {
        this.experienceAdminAccount = experienceAdminAccount;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public int getIfWxUsers() {
        return ifWxUsers;
    }

    public void setIfWxUsers(int ifWxUsers) {
        this.ifWxUsers = ifWxUsers;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getHeadimgurl() {
        return headimgurl;
    }

    public void setHeadimgurl(String headimgurl) {
        this.headimgurl = headimgurl;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public int getExpireDay() {
        return expireDay;
    }

    public void setExpireDay(int expireDay) {
        this.expireDay = expireDay;
    }
}
